﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceManager.Utils;
namespace QuotationService.RealtimeQuotation
{
    public class OptionQuotationRepository : QuotationRepositoryBase
    {
        private static OptionEntityConverter sptionEntityConverter = new OptionEntityConverter();

        #region 基本信息操作
        /// <summary>
        /// 更新单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="basicInfo"></param>
        /// <returns></returns>
        public static bool UpdateBasicInfo(string quotSource, Quotation.Option.BasicInfo basicInfo)
        {
            OptionBasicInfo sptionBasicInfo = sptionEntityConverter.Convert(basicInfo);
            byte[] bytes = sptionBasicInfo.StructToBytes<OptionBasicInfo>();
            return BasicInfoChannel.Update(quotSource, sptionBasicInfo.Code, bytes);
        }
        /// <summary>
        /// 获取单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Quotation.Option.BasicInfo GetBasicInfo(string quotSource, string code)
        {
            var bytes = BasicInfoChannel.Get(quotSource, code);
            OptionBasicInfo sptionBasicInfo = StructHelper.BytesToStruct<OptionBasicInfo>(bytes);
            return sptionEntityConverter.Convert(sptionBasicInfo);
        }
        /// <summary>
        /// 获取全部商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Option.BasicInfo> GetAllBasicInfo(string quotSource)
        {
            var allBasicInfoBytes = BasicInfoChannel.GetAll(quotSource);
            List<Quotation.Option.BasicInfo> rtn = new List<Quotation.Option.BasicInfo>();
            foreach (byte[] bytes in allBasicInfoBytes)
            {
                OptionBasicInfo sptionBasicInfo = StructHelper.BytesToStruct<OptionBasicInfo>(bytes);
                rtn.Add(sptionEntityConverter.Convert(sptionBasicInfo));
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearBasicInfo(string quotSource, string code)
        {
            return BasicInfoChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的基本信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearAllBasicInfo(string quotSource)
        {
            return BasicInfoChannel.Clear(quotSource);
        }
        #endregion

        #region 行情信息操作
        /// <summary>
        /// 更新单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="quotInfo"></param>
        /// <param name="appendToHistoryList"></param>
        /// <returns></returns>
        public static bool UpdateQuotationInfo(string quotSource, Quotation.Option.QuotationInfo quotInfo, bool appendToHistoryList)
        {
            OptionQuotationInfo sptionQuotInfo = sptionEntityConverter.Convert(quotInfo);
            byte[] bytes = sptionQuotInfo.StructToBytes<OptionQuotationInfo>();
            return QuotationInfoChannel.Update(quotSource, quotInfo.StkId.LongCode, bytes, appendToHistoryList);
        }
        /// <summary>
        /// 获取单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Quotation.Option.QuotationInfo GetQuotationInfo(string quotSource, string code)
        {
            var bytes = QuotationInfoChannel.Get(quotSource, code);
            OptionQuotationInfo sptionQuotationInfo = StructHelper.BytesToStruct<OptionQuotationInfo>(bytes);
            return sptionEntityConverter.Convert(sptionQuotationInfo);
        }
        /// <summary>
        /// 获取全部商品的行情信息快照
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Option.QuotationInfo> GetQuotationInfoSnap(string quotSource)
        {
            var allQuotInfoBytes = QuotationInfoChannel.GetAll(quotSource);
            List<Quotation.Option.QuotationInfo> rtn = new List<Quotation.Option.QuotationInfo>();
            foreach (byte[] bytes in allQuotInfoBytes)
            {
                OptionQuotationInfo sptionQuotationInfo = StructHelper.BytesToStruct<OptionQuotationInfo>(bytes);
                rtn.Add(sptionEntityConverter.Convert(sptionQuotationInfo));
            }
            return rtn;
        }
        /// <summary>
        /// 获取单个商品的当日历史行情
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<Quotation.Option.QuotationInfo> GetHistoryQuotationInfo(string quotSource, string code)
        {
            var allQuotInfoBytes = QuotationInfoChannel.GetAll(quotSource, code);
            List<Quotation.Option.QuotationInfo> rtn = new List<Quotation.Option.QuotationInfo>();
            foreach (byte[] bytes in allQuotInfoBytes)
            {
                OptionQuotationInfo sptionQuotationInfo = StructHelper.BytesToStruct<OptionQuotationInfo>(bytes);
                rtn.Add(sptionEntityConverter.Convert(sptionQuotationInfo));
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearQuotionInfo(string quotSource, string code)
        {
            return QuotationInfoChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的行情信息
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearAll(string quotSource)
        {
            return QuotationInfoChannel.ClearAll(quotSource);
        }
        #endregion

        #region Tick信息操作
        /// <summary>
        /// 发布Tick
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <param name="tick"></param>
        /// <returns></returns>
        public static bool AppendTick(string quotSource, string code, Quotation.Option.TickInfo tick)
        {
            OptionTickInfo tickInfoEntity = sptionEntityConverter.Convert(tick);
            byte[] bytes = tickInfoEntity.StructToBytes<OptionTickInfo>();
            return QuotationInfoChannel.UpdateTick(quotSource, code, bytes);
        }
        /// <summary>
        /// 获取单个商品的当日历史Tick
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns></returns>
        public static List<Quotation.Option.TickInfo> GetTicks(string quotSource, string code, int? startIndex, int? endIndex)
        {
            if (!startIndex.HasValue)
            {
                startIndex = 0;
            }
            if (!endIndex.HasValue)
            {
                endIndex = -1;
            }
            List<byte[]> tickBytesList = QuotationInfoChannel.GetTicks(quotSource, code, startIndex.Value, endIndex.Value);
            List<Quotation.Option.TickInfo> rtn = new List<Quotation.Option.TickInfo>();
            foreach (var bytes in tickBytesList)
            {
                OptionTickInfo tickInfoEntity = StructHelper.BytesToStruct<OptionTickInfo>(bytes);
                rtn.Add(sptionEntityConverter.Convert(tickInfoEntity));
            }
            return rtn;
        }

        #endregion

        #region 分线数据操作
        /// <summary>
        /// 更新单个商品的分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="minuteData"></param>
        /// <returns></returns>
        public static bool UpdateMinuteData(string quotSource, Quotation.Option.MinuteData minuteData)
        {
            OptionMinuteData sptionMinuteData = sptionEntityConverter.Convert(minuteData);
            byte[] bytes = sptionMinuteData.StructToBytes<OptionMinuteData>();
            return MinuteDataChannel.Update(quotSource, sptionMinuteData.Code, sptionMinuteData.OccurTime, bytes);
        }
        /// <summary>
        /// 获取单个商品当日历史分线
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<Quotation.Option.MinuteData> GetMinuteData(string quotSource, string code)
        {
            List<byte[]> minuteBytesList = MinuteDataChannel.GetAll(quotSource, code);
            List<Quotation.Option.MinuteData> rtn = new List<Quotation.Option.MinuteData>();
            foreach (var bytes in minuteBytesList)
            {
                OptionMinuteData sptionMinuteData = StructHelper.BytesToStruct<OptionMinuteData>(bytes);
                rtn.Add(sptionEntityConverter.Convert(sptionMinuteData));
            }
            return rtn;
        }
        /// <summary>
        /// 获取全部商品的分线数据快照
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Option.MinuteData> GetMinuteDataSnap(string quotSource)
        {
            List<byte[]> minuteBytesList = MinuteDataChannel.GetAll(quotSource);
            List<Quotation.Option.MinuteData> rtn = new List<Quotation.Option.MinuteData>();
            foreach (var bytes in minuteBytesList)
            {
                OptionMinuteData sptionMinuteData = StructHelper.BytesToStruct<OptionMinuteData>(bytes);
                rtn.Add(sptionEntityConverter.Convert(sptionMinuteData));
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的全部分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearMinuteData(string quotSource, string code)
        {
            return MinuteDataChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的最新分线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearMinuteDataSnap(string quotSource)
        {
            return MinuteDataChannel.ClearAll(quotSource);
        }
        #endregion

        #region 日线数据操作
        /// <summary>
        /// 更新单个商品的日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="dailyData"></param>
        /// <returns></returns>
        public static bool UpdateDailyData(string quotSource, Quotation.Option.DailyData dailyData)
        {
            OptionDailyData sptionDailyData = sptionEntityConverter.Convert(dailyData);
            byte[] bytes = sptionDailyData.StructToBytes<OptionDailyData>();
            return DailyDataChannel.Update(quotSource, dailyData.StkId.LongCode, bytes);
        }
        /// <summary>
        /// 获取单个商品的最新日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Quotation.Option.DailyData GetDailyData(string quotSource, string code)
        {
            byte[] bytes = DailyDataChannel.Get(quotSource, code);
            OptionDailyData sptionDailyData = StructHelper.BytesToStruct<OptionDailyData>(bytes);
            return sptionEntityConverter.Convert(sptionDailyData);
        }
        /// <summary>
        /// 获取全部商品的最新日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static List<Quotation.Option.DailyData> GetDailyDataSnap(string quotSource)
        {
            List<byte[]> dailyBytesList = DailyDataChannel.GetAll(quotSource);
            List<Quotation.Option.DailyData> rtn = new List<Quotation.Option.DailyData>();
            foreach (var bytes in dailyBytesList)
            {
                OptionDailyData sptionDailyData = StructHelper.BytesToStruct<OptionDailyData>(bytes);
                rtn.Add(sptionEntityConverter.Convert(sptionDailyData));
            }
            return rtn;
        }
        /// <summary>
        /// 清除单个商品的日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool ClearDailyData(string quotSource, string code)
        {
            return DailyDataChannel.Clear(quotSource, code);
        }
        /// <summary>
        /// 清除全部商品的日线数据
        /// </summary>
        /// <param name="quotSource"></param>
        /// <returns></returns>
        public static bool ClearDailyDataSnap(string quotSource)
        {
            return DailyDataChannel.ClearAll(quotSource);
        }
        #endregion

    }
}
